package com.xh.bussiness.resource.controller;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xh.bussiness.resource.bean.request.MaterialCourseIndexRequest;
import com.xh.bussiness.resource.bean.request.MaterialSaveRequest;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.util.DateUtils;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.xh.bussiness.resource.bean.request.MaterialRequest;
import com.xh.bussiness.resource.model.Material;
import com.xh.bussiness.resource.service.IMaterialService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 资料 前端控制器
 * </p>
 *
 * @author JQM
 * @since 2019-08-27
 */
@RestController
@RequestMapping("/admin/material")
public class MaterialController extends BaseController {

    @Autowired
    private IMaterialService materialService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(@RequestBody MaterialRequest bean){
        XhPage page = new XhPage();
        page.setCurrent(bean.getCurrent());
        page.setSize(bean.getSize());
        page=materialService.queryMaterialPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(MaterialRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.queryMaterial(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(MaterialRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=materialService.exportAll(request,exportHeaderBeans,bean,"queryMaterialPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param material 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated @RequestBody Material material){
        if(material.getId()==null){
            materialService.insert(material);
        }else{
            materialService.update(material);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",material);
    }

    /**
     * 批量保存
     * @param materials 实体对象数组
     * @return
     */
    @PostMapping(value="/batchSave")
    public AjaxResult batchSave(@Validated @RequestBody MaterialSaveRequest materials,HttpServletRequest request){
        if(CollectionUtils.isEmpty(materials.getFileList())){

            return new AjaxResult(AjaxResultConstant.Result.ERROR,"文件内容丢失，请联系管理员");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", materialService.batchSave(materials,request));
    }

    /**
     * 保存个人上传的资料
     * @param materials 实体对象数组
     * @return
     */
    @PostMapping(value="/saveUserMaterial")
    public AjaxResult saveUserMaterial(@Validated @RequestBody MaterialSaveRequest materials,HttpServletRequest request){
        if(CollectionUtils.isEmpty(materials.getFileList())){

            return new AjaxResult(AjaxResultConstant.Result.ERROR,"文件内容丢失，请联系管理员");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, materialService.saveUserMaterial(materials,request));
    }

    /**
     * 保存创建的讲义
     * @param materials 实体对象数组
     * @return
     */
    @PostMapping(value="/saveMaterial")
    public AjaxResult saveMaterial(@Validated @RequestBody MaterialRequest materials,HttpServletRequest request){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, materialService.saveMaterial(materials,request));
    }

    /**
     * 复制的讲义
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/copyMaterial")
    public AjaxResult copyMaterial(@Validated MaterialRequest request){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,materialService.copyMaterial(request));
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        materialService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        materialService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     *  查询课次绑定的资料
     * @param bean
     * @return
     */
    @PostMapping("/queryMaterialByCourseIndexId")
    public AjaxResult queryMaterialByCourseIndexId(MaterialCourseIndexRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.queryMaterialByCourseIndexId(bean));
    }

//    @ExcludeLoginTokenIntercept
    @PostMapping("/queryCountByType")
    public AjaxResult queryCountByType(@RequestBody MaterialRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.queryCountByType(bean));
    }

    /**
     *  查询资料绑定的章节名称
     * @param bean
     * @return
     */
    @PostMapping("/queryChapterById")
    public AjaxResult queryChapterById(MaterialCourseIndexRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.queryChapterById(bean));
    }

    /**
     *  查询资料绑定的章节名称
     * @param id
     * @return
     */
    @PostMapping("/queryById")
    public AjaxResult queryById(String id){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.getById(id));
    }

    /**
     *  查询资料绑定的章节名称
     * @param bean
     * @return
     */
    @PostMapping("/queryUserPrivateMaterial")
    public AjaxResult queryUserPrivateMaterial(@RequestBody MaterialRequest bean){
        bean.setCreatorId(UserUtils.getCurrentUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,materialService.queryUserPrivateMaterial(bean));
    }

    /*
     * 下载讲义
     * @author 赵文博 
     * @date 2021/5/15 10:42
     * @param materialId
     * @param request
     * @param fileType
     * @param response
     * @return void
     **/
    @ExcludeLoginTokenIntercept
    @GetMapping("/down")
    public void down(String materialId, String fileType, Integer type, HttpServletRequest request, HttpServletResponse response) throws Exception {
        materialService.downMaterial(materialId,fileType,type,response,request);
    }

    @PostMapping("/update")
    public AjaxResult update(HttpServletRequest request, @RequestBody Material material){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",materialService.updateMaterial(request, material));
    }
}

